package edu.neu.cloud_factory.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.neu.cloud_factory.dao.DeviceCapacityMapper;
import edu.neu.cloud_factory.entity.po.Device;
import edu.neu.cloud_factory.entity.po.DeviceCapacity;
import edu.neu.cloud_factory.entity.po.Product;
import edu.neu.cloud_factory.entity.vo.DeviceCapacityVo;
import edu.neu.cloud_factory.service.DeviceCapacityService;
import edu.neu.cloud_factory.service.DeviceService;
import edu.neu.cloud_factory.service.ProductService;
import edu.neu.cloud_factory.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 设备产能 前端控制器
 * </p>
 *
 * @author halozhy
 * @since 2021-07-15
 */
@RestController
@RequestMapping("/api/dc")
public class DeviceCapacityController {
    @Autowired
    DeviceCapacityService deviceCapacityService;

    @Autowired
    ProductService productService;

    @Autowired
    DeviceService deviceService;

    @GetMapping("/list_by_device_id")
    public List<DeviceCapacityVo> listByDeviceId(long deviceId) {
        QueryWrapper<DeviceCapacity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("c_device_id", deviceId);
        List<DeviceCapacity> deviceCapacityList = deviceCapacityService.list(queryWrapper);

        List<Product> productList = productService.list();
        Map<Long, Product> productMap = new HashMap<>();
        productList.forEach(product -> {
            productMap.put(product.getPId(), product);
        });

        List<DeviceCapacityVo> deviceCapacityVoList = new ArrayList<>();
        for (DeviceCapacity deviceCapacity :
                deviceCapacityList) {
            DeviceCapacityVo deviceCapacityVo = new DeviceCapacityVo();
            deviceCapacityVo.setId(deviceCapacity.getCId());
            deviceCapacityVo.setDevice_id(deviceCapacity.getCDeviceId());
            deviceCapacityVo.setNum(deviceCapacity.getCNum());
            deviceCapacityVo.setProduct_id(deviceCapacity.getCProductId());
            deviceCapacityVo.setProduct_name(productMap.get(deviceCapacity.getCProductId()).getPName());
            deviceCapacityVoList.add(deviceCapacityVo);
        }
        return deviceCapacityVoList;
    }

    @PostMapping("/update")
    public int update(@RequestBody String CONTENT) {
        JSONObject j = JSONObject.parseObject(CONTENT);
        if (Utils.checkEmpty(j, Arrays.asList("id", "device_id", "product_id", "num"))) {
            DeviceCapacity deviceCapacity = deviceCapacityService.getById(j.getLong("id"));
            if (deviceCapacity == null) {
                return -1; // 找不到id
            } else {
//                QueryWrapper<DeviceCapacity> queryWrapper = new QueryWrapper<>();
//                queryWrapper.eq("c_device_id", j.getString("device_id"));
//                queryWrapper.eq("c_product_id", j.getString("product_id"));
//                List<DeviceCapacity> deviceCapacityList = deviceCapacityService.list(queryWrapper);
//                if (deviceCapacityList.size() != 0){
//                    return -5; // 已存在此产品的产能配置，请使用修改
//                }
                deviceCapacity.setCDeviceId(j.getLong("device_id"));
                deviceCapacity.setCProductId(j.getLong("product_id"));
                deviceCapacity.setCNum(j.getLong("num"));
                deviceCapacityService.updateById(deviceCapacity);
                return 0;
            }
        }
        return -2;
    }

    @GetMapping("/delete")
    public int delete(long id) {
        if (deviceCapacityService.getById(id) != null){
            deviceCapacityService.removeById(id);
            return 0;
        }
        return -1;
    }

    @PostMapping("/add")
    public int add(@RequestBody String CONTENT) {
        JSONObject j = JSONObject.parseObject(CONTENT);
        if (Utils.checkEmpty(j, Arrays.asList("device_id", "product_id", "num"))) {
            // device_id 验证
            Device d = deviceService.getById(j.getLong("device_id"));
            if (d == null){
                return -3;
            }
            // product_id 验证
            Product product = productService.getById(j.getLong("product_id"));
            if (product == null){
                return -4;
            }
            QueryWrapper<DeviceCapacity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("c_device_id", d.getDId());
            queryWrapper.eq("c_product_id", product.getPId());
            List<DeviceCapacity> deviceCapacityList = deviceCapacityService.list(queryWrapper);
            if (deviceCapacityList.size() != 0){
                return -5; // 已存在此产品的产能配置，请使用修改
            }
            DeviceCapacity deviceCapacity = new DeviceCapacity();
            deviceCapacity.setCDeviceId(j.getLong("device_id"));
            deviceCapacity.setCProductId(j.getLong("product_id"));
            deviceCapacity.setCNum(j.getLong("num"));
            deviceCapacityService.save(deviceCapacity);
            return 0;
        }
        return -2;
    }
}

